import {createRouter, createWebHistory} from 'vue-router'
import loginForm from "@/page/login-form";
import registerFrame from "@/page/register-frame";
import userCenterInfo from "@/page/user-center-info";
import insideLayout from "@/components/inside-layout";
import app from "@/App";
import reCharge from "@/page/ReCharge";
import TradeForm from "@/page/trade-form";
import NFT from "@/page/NFT";
import myOnsale from "@/page/my-onsale";
import verificateOnChain1 from "@/components/VerificateOnChain1";
import msgBox from "@/components/msgBox";
import tradeRecordManager from "@/page/trade-record-manager";
import newsCrud from "@/page/news-crud";
import caseCrud from "@/page/case-crud";
import systemFeedback from "@/page/system-feedback";
import tradeManage from "@/page/trade-manage";
import feedbackForm from "@/page/feedback-form";

const routes = [
    {
        path: '/main',
        component: app,
        redirect: '/tradeCenter', //TODO 一开始就进入轮播页面
        children: [
            {
                path: '/register',
                component: registerFrame
            },
            {
                path: '/login',
                component: loginForm
            },
            {
                path: '/home',
                component: () => import('../page/HomeFrame.vue')
            }
        ]
    },
    {
        path: '/userCenter',
        component: insideLayout,
        redirect: '/userInfo',
        children: [
            {
                path: '/userInfo',
                component: userCenterInfo,
            },
            {
                path: '/messageBox',
                component: msgBox
            },
            {
                path: '/nft',
                component: NFT
            },
            {
                path: '/tradeRecord',
                component: TradeForm
            },
            {
                path: '/sale',
                component: myOnsale
            },
            {
                path: '/feedbackForm',
                component: feedbackForm,
            },
        ]
    },
    {
        path: '/inside',
        component: () => import('../components/only-topmenu-layout.vue'),
        redirect: '/recharge',
        children: [
            {
                path: '/recharge',
                component: reCharge,
            },
            {
                path: '/verificateOnChain',
                component: verificateOnChain1,
            },
            {
                path: '/tradeCenter',
                component: () => import('../page/trade-center.vue')
            },
            {
                path: '/goodsDetail/:id',
                name: 'goodsDetail',
                component: () => import(/* webpackChunkName: "dashboard" */ '../page/goodsDetail.vue'),

            },
        ]
    },
    {
        path: '/',
        redirect: '/main',
    },
    {
        path: '/findPassword',
        component: () => import('../components/find-password.vue')
    },
    {
        path: '/changePassword',
        component: () => import('../components/change-password.vue')
    },
    {
        path: '/manager',
        component: () => import('../page/manager.vue'),
        redirect: '/tradeRecordManager',
        children: [
            {
                path: '/tradeRecordManager',
                component: tradeRecordManager,
            },
            {
                path: '/newsCrud',
                component: newsCrud,
            },
            {
                path: '/caseCrud',
                component: caseCrud,
            },
            {
                path: '/systemFeedback',
                component: systemFeedback
            },
            {
                path: '/tradeManage',
                component: tradeManage
            }
        ]
    },
]

/**
 * 定义返回模块
 */
const router = createRouter({
    history: createWebHistory('/untitled4/'),
    routes
})

//路由全局前置守卫
//需要支持游客模式才行
router.beforeEach((to, from, next) => {
    if (to.path == '/register' || to.path == '/login' || to.path == '/home' || to.path == '/findPassword') {
        next()
    }

/*    if (to.path != '/tradeCenter') {
        if (JSON.parse(sessionStorage.getItem('userObj')).id == 11) {
            next({
                path: '/login',
                query: {
                    redirect: to.fullPath
                }
            })
        }
        next()
    }*/

    next()

    if (sessionStorage.getItem('token')) {
        console.log("@@@###")
        next()
    } else {
        next({
            path: '/login',
            query: {
                redirect: to.fullPath
            }
        })
    }


})

export default router

